package server;

import java.net.*;
import java.io.*;

class ServerThread extends Thread 
{
    private Socket socket = null;
    private SocketManager socketManager = null;

    public ServerThread(Socket socket, SocketManager socketManager) 
    {
        this.socket = socket;
        this.socketManager = socketManager;
    }

    public void run() 
    {
        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String inputLine;
            
            while ((inputLine = in.readLine()) != null) {
                socketManager.sendToAll(inputLine.trim());
                if (inputLine.equals("bye")) {
                    break;
                }
            }
            out.close();
            in.close();
            socketManager.remove(socket);
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
